package com.whu.tcx01.utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

public class SaveAsTxt {
    public static void exportTxt(Double d , int n){
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;
        try {
            File file = new File("D:\\Codes\\txt\\car"+n+".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            dos.write(String.valueOf(d)+"\n");

            System.out.println("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void exportTxt2(List<Double> tList,int n) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;
        try {
            File file = new File("D:\\Codes\\txt\\probs\\car"+n+".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (Double aDouble : tList) {
//                System.out.println(aDouble);

                dos.write(String.valueOf(aDouble)+"\n");
            }

            System.out.println("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void exportTxt3(List<String> sList,int n) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;
        try {
            File file = new File("D:\\Codes\\txt\\coords\\car"+n+".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (String s : sList) {
                String[] canArr1 = s.split(",");
                double x = Double.parseDouble(canArr1[0]);
                double y = Double.parseDouble(canArr1[1]);
                dos.write(x+","+y+"\n");
            }

            System.out.println("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //最终结果点坐标
    public static void exportTxt4(List<String> tList) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;
        try {
            File file = new File("D:\\Codes\\txt\\results\\cars.txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (String t : tList) {
                String[] canArr1 = t.split(",");
                double x = Double.parseDouble(canArr1[0]);
                double y = Double.parseDouble(canArr1[1]);
                dos.write(x+","+y+"\n");
            }
            System.out.println("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void exportTxt5(List<String> tList,String str) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;

        try {
            File file = new File("D:\\Codes\\txt\\originalCoords\\cars_"+str+".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (String t : tList) {
                String[] canArr1 = t.split(",");
                double x = Double.parseDouble(canArr1[0]);
                double y = Double.parseDouble(canArr1[1]);
                dos.write(x+","+y+"\n");
            }
            System.out.println("保存完成！");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //导出观测概率
    public static void exportTxt6(List<Double> tList,int n) {
        FileOutputStream fos = null;
        OutputStreamWriter dos = null;

        try {
            File file = new File("D:\\Codes\\txt\\obse\\carObservation"+n+".txt");
            if (!file.exists()) {
                file.createNewFile();
            }

            fos = new FileOutputStream(file);
            dos = new OutputStreamWriter(fos);

            for (Double aDouble : tList) {
//                System.out.println(aDouble);

                dos.write(String.valueOf(aDouble)+"\n");
            }

            System.out.println("保存完成！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.flush();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
